package cn.nnxy.zentao.controller;

import cn.nnxy.zentao.domain.ZNeed;
import cn.nnxy.zentao.service.NeedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/*
 * @ build by 何昌舜
 * @ file name 需求controller
 */

@RequestMapping("/product")
@Controller
public class NeedController {
    @Autowired
    private NeedService needService;

    @GetMapping("need")
    public String needUI() {
        return "";
    }

    @PostMapping("need")
    public String need(){

        return "";
    }
    //新建需求
    @GetMapping("needAdd")
    public String needAddUI(){
        return "";
    }

    @PostMapping("needAdd")
    public String needAdd(ZNeed need, BindingResult result, Model model){
        if (result.hasErrors()){
            model.addAttribute("need",need);
            model.addAttribute("msg", result.getAllErrors().get(0).getDefaultMessage());
            return "";
        }
        int temp = needService.saveNeed(need);
        if (temp == 1){
            model.addAttribute("msg", "添加成功！");
            return "redirect:";
        }else {
            model.addAttribute("msg", "添加失败！");
            return "";
        }

    }

    //删除需求
    @ResponseBody
    @PostMapping("needDel")
    public boolean needDel(Integer needId){
        if (null == needId){
            return false;
        }
        return needService.deleteById(needId) > 0;
    }

    //编辑需求
    @GetMapping("needEdit")
    public String needEditUI(Integer needId, Model model){
        ZNeed need = needService.seletById(needId);
        model.addAttribute("need", need);
        return "";
    }

    @PostMapping("needEdit")
    public String needEdit(ZNeed need, BindingResult result, Model model){
        if (result.hasErrors()){
            model.addAttribute("need", need);
            model.addAttribute("msg", result.getAllErrors());
            return "";
        }
        int temp = needService.updateById(need);
        if (temp == 1){
            model.addAttribute("msg", "修改成功。");
            return "redirect:";
        }else {
            model.addAttribute("msg", "修改失败。");
            return "";
        }

    }

}
